﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using GiaPhuc.Data;

namespace GiaPhuc.Admin
{
    public partial class TopicEntry : System.Web.UI.Page
    {
        private int PageID
        {
            get
            {
                if (null != Request["ID"])
                {
                    return int.Parse(Request["ID"]);
                }
                return 0;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ddlNewsCategory.DataSource = define.PageTypes;
                this.ddlNewsCategory.DataBind();

                using (IDbConnection connection = new MySql.Data.MySqlClient.MySqlConnection(Helper.ConnectionString))
                {
                    using (ResellEraDMInPhUkIeNipAdX context = new ResellEraDMInPhUkIeNipAdX(connection))
                    {
                        int pageID = PageID;
                        var page = context.EZPages.SingleOrDefault(x => x.PagesID == pageID);
                        if (null != page)
                        {
                            this.txtTitle.Text = page.PagesTitle;
                            this.ddlNewsCategory.SelectedIndex = page.ToCChapter;
                            this.elm1.Value = page.PagesHTMLText.RemoveBadCode();
                        }
                    }
                }
            }
        }

        protected void lnkSave_Click(object sender, EventArgs e)
        {
            try
            {
                using (IDbConnection connection = new MySql.Data.MySqlClient.MySqlConnection(Helper.ConnectionString))
                {
                    using (ResellEraDMInPhUkIeNipAdX context = new ResellEraDMInPhUkIeNipAdX(connection))
                    {
                        if (PageID == 0)
                        {
                            EZPages ezpage = new EZPages
                            {
                                ALtURL = String.Empty,
                                ALtURLExternal = String.Empty,
                                PagesTitle = this.txtTitle.Text,
                                PagesHTMLText = this.elm1.Value.RemoveBadCode(),
                                ToCChapter = this.ddlNewsCategory.SelectedIndex
                            };

                            context.EZPages.InsertOnSubmit(ezpage);
                        }
                        else
                        {
                            int pageID = PageID;
                            EZPages ezpage = context.EZPages.SingleOrDefault(x => x.PagesID == pageID);

                            ezpage.PagesTitle = this.txtTitle.Text;
                            ezpage.PagesHTMLText = this.elm1.Value.RemoveBadCode();
                            ezpage.ToCChapter = this.ddlNewsCategory.SelectedIndex;
                        }

                        context.SubmitChanges();
                    }
                }

                if (PageID == 0)
                {
                    Response.Redirect("/Admin/Pages/TopicEntry.aspx");
                }
                else
                {
                    Response.Redirect("/Admin/Pages/TopicManager.aspx");
                }
            }
            catch
            {
                throw;
            }
        }

    }
}